package com.lxj.cms.manage.action;

import com.lxj.cms.manage.entity.ProductBrandsEntity;
import com.lxj.cms.manage.entity.ProductCategoriesEntity;
import com.lxj.cms.manage.entity.ProductNormalEntity;

public class InitAction extends ManageBaseAction {

	private static final long serialVersionUID = -4115495484310213022L;

	@Override
	public String execute() throws Exception {
		try {
			// 初始化分类
			ProductCategoriesEntity root = new ProductCategoriesEntity();
			root.setInfoTitle("箱包");
			this.getBaseService().save(root);

			for (int i = 0; i < 10; i++) {
				ProductCategoriesEntity temp = new ProductCategoriesEntity();
				temp.setInfoTitle("小箱包" + i);
				temp.setParent(root);
				this.getBaseService().save(temp);
				initBraed(temp);
				root.addChild(temp);
			}

			this.getBaseService().saveOrUpdate(root);

			root = new ProductCategoriesEntity();
			root.setInfoTitle("数码");
			this.getBaseService().save(root);

			for (int i = 0; i < 5; i++) {
				ProductCategoriesEntity temp = new ProductCategoriesEntity();
				temp.setInfoTitle("数码" + i);
				temp.setParent(root);
				this.getBaseService().save(temp);
				initBraed(temp);
				root.addChild(temp);
			}

			root = new ProductCategoriesEntity();
			root.setInfoTitle("音像");
			this.getBaseService().save(root);
			root = new ProductCategoriesEntity();
			root.setInfoTitle("酒水");
			this.getBaseService().save(root);
			root = new ProductCategoriesEntity();
			root.setInfoTitle("办公");
			this.getBaseService().save(root);

			wirteResponse("初始化成功");
		} catch (Exception e) {
			e.printStackTrace();
			wirteResponse("初始化失败" + e.getLocalizedMessage());
		}
		return null;
	}

	private void initBraed(ProductCategoriesEntity paret) {
		for (int i = 0; i < 5; i++) {
			ProductBrandsEntity temp = new ProductBrandsEntity();
			temp.setInfoTitle(paret.getInfoTitle() + "品牌" + 1);
			temp.setCategorie(paret);
			this.getBaseService().save(temp);
			initProduct(paret, temp);
		}

	}

	/**
	 * 初始化产品 函数功能说明 lxj 2013-8-22 修改者名字 修改日期 修改内容
	 * 
	 * @参数：
	 * @return void
	 * @throws
	 */
	public void initProduct(ProductCategoriesEntity categories, ProductBrandsEntity brands) {
		for (int i = 0; i < 10; i++) {
			ProductNormalEntity temp = new ProductNormalEntity();
			temp.setAbsoluteUrl("http://www.baidu.com");
			temp.setImageUrl(this.getProjectURL() + "/images/test.jpg");
			temp.setInfoTitle(brands.getInfoTitle() + "商品1" + i + categories.getInfoTitle());
			temp.setInfoPrice("10" + i + "0.1");
			temp.setInfoScore(i);
			temp.setInfoComment(i + 10);
			temp.setInfoSource("淘宝网");
			temp.setInfoPostedType(1);
			temp.setCategorie(categories);
			temp.setBrands(brands);
			this.getBaseService().save(temp);
		}
	}

}
